package com.zhiyou100.springbootreidsquartz.job;

import com.zhiyou100.springbootreidsquartz.mapper.IActorMapper;
import com.zhiyou100.springbootreidsquartz.pojo.Actor;
import com.zhiyou100.springbootreidsquartz.util.RedisUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 定时任务(定时把MySQL中的数据同步redis)
 */
public class RedisJob implements Job {
    @Resource
    private IActorMapper actorMapper;
    @Resource
    private RedisUtils redisUtils;

    //从MySQL获取数据同步到redis
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //存入Redis
        /*从MySQL中拿数据,调用mapper层*/
        List<Actor> actors = actorMapper.selectList(null);
        //向redis中存一份
        for (Actor actor : actors) {
            HashMap<String,Object> map = new HashMap<>();
            map.put("id",actor.getId());
            map.put("name",actor.getName());
            map.put("phone",actor.getDesc());
            redisUtils.hmset("actor:id:"+actor.getId(),map);
        }
    }
}
